// pages/order/order.js
import { request } from "../../request/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        value: "全部订单",
        id: 1,
        isActive: true,
      },
      {
        value: "已付款",
        id: 2,
        isActive: false,
      },
      {
        value: "未付款",
        id: 3,
        isActive: false,
      },
      {
        value: "退款/退货",
        id: 4,
        isActive: false,
      },
    ],
    NowActive: 0,
    // 订单列表
    orders: [],
  },
  //
  //
  //
  tabClickChange(index) {
    let { tabs } = this.data;
    tabs.forEach((Item) =>
      index.detail == Item.id
        ? (Item.isActive = true)
        : (Item.isActive = false)
    );
   
    this.getTypeList(index.detail);
    this.setData({
      tabs: tabs,
      NowActive: index.detail,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let token = wx.getStorageSync("token");
    if (!token) {
      wx.navigateTo({ url: "/pages/auth/auth" });
      return;
    }
  },
  onShow(e) {
    // 获取页面栈
    let pages = getCurrentPages();
    // 获取当前页面
    let currentPage = pages[pages.length - 1];
    // 当前 传入参数值
    let type = currentPage.options.type;

    // 改变nav导航活跃按钮
    this.tabClickChange({ detail: type });
  },
  // 获取订单列表
  async getTypeList(type) {
    let _res = await request({
      url: "/my/orders/all",
      method: "get",
      data: {
        type: type,
      },
    })||{};
    let _new_res =  _res.data.message.orders.map((item)=>{
       item.cn_date = new Date(item.create_time*1000).toLocaleString()
       return item
    })

    this.setData({
      orders: _new_res,
    });
  },
});
